package com.listeningframework.boot.autoconfigure.template.freemarker.templates.security;

import java.io.IOException;
import java.util.Map;

import com.listeningframework.boot.autoconfigure.security.authentication.SpringSecurityUtils;
import com.listeningframework.boot.autoconfigure.template.freemarker.overrides.ListeningStandardFreemarkerTagSupport;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 
 * Spring Security Freemarker标签
 *
 * @author lixining
 * @version $Id: SecurityHasPermission.java, v 0.1 2016年12月21日 上午11:36:49 lixining Exp $
 */
public class SecurityHasPermission extends ListeningStandardFreemarkerTagSupport {

    @Override
    @SuppressWarnings("rawtypes")
    public void execute(Environment env, Map params, TemplateModel[] loopVars,
                        TemplateDirectiveBody body) throws TemplateException, IOException {
        String permission = getParams(params, "value");
        if (!SpringSecurityUtils.hasPermission(permission)) {
            return;
        }
        if (body != null) {
            body.render(env.getOut());
        }
    }

}
